package com.itcast.googleplay.holder;

import java.text.Format;

import com.itcast.googleplay.R;
import com.itcast.googleplay.bean.AppInfo;
import com.itcast.googleplay.http.HttpApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class AppInfoHolder extends BaseHolder<AppInfo>{

	private ImageView ivIcon;
	private TextView tvName,tvDownloadNumber,tvVersion,tvPubData,tvSize;
	private RatingBar ratingBar;
	private DisplayImageOptions options;

	@Override
	public void initData() {
		AppInfo appInfo = getData();
		tvName.setText(appInfo.name);
		ratingBar.setRating(appInfo.stars);
		tvDownloadNumber.setText(appInfo.downloadNum);
		tvVersion.setText(appInfo.version);
		tvPubData.setText(appInfo.date);
		tvSize.setText(Formatter.formatFileSize(UIUtils.getContext(), appInfo.size));
		
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)
			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)//是否允许让图片反转
			.build();
		ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+appInfo.getIconUrl(), ivIcon, options);
	}

	@Override
	public View initView() {
		View view = UIUtils.inflate(R.layout.layout_app_info);
		ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
		tvName = (TextView) view.findViewById(R.id.tv_name);
		ratingBar = (RatingBar) view.findViewById(R.id.ratingbar);

		tvDownloadNumber = (TextView) view.findViewById(R.id.tv_download_number);
		tvVersion = (TextView) view.findViewById(R.id.tv_version);
		tvPubData = (TextView) view.findViewById(R.id.tv_pub_data);
		tvSize = (TextView) view.findViewById(R.id.tv_size);
		
		return view;
	}

}
